package com.changgou.goods.service.impl;

import com.changgou.goods.dao.BrandMapper;
import com.changgou.goods.pojo.Brand;
import com.changgou.goods.service.BrandService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    private BrandMapper brandMapper;

    private Example createExample(Brand brand){
        Example example=new Example(Brand.class);
        Example.Criteria criteria = example.createCriteria();
        if (brand.getId()!=null){
            criteria.andEqualTo("id",brand.getId());
        }
        if (!StringUtils.isEmpty(brand.getName())){
            criteria.andLike("name","%"+brand.getName()+"%");
        }
        if (!StringUtils.isEmpty(brand.getImage())){
            criteria.andEqualTo("image",brand.getImage());
        }
        if (!StringUtils.isEmpty(brand.getLetter())){
            criteria.andEqualTo("letter",brand.getLetter());
        }
        if (!StringUtils.isEmpty(brand.getSeq())){
            criteria.andEqualTo("seq",brand.getSeq());
        }
        return example;
    }



    /**
     *     * 查询全部品牌
     *     * @return 品牌列表
     *    
     */
    @Override
    public List<Brand> findAll() {
        return brandMapper.selectAll();
    }

    /**
     * 根据id查询数据
     * @return
     */
    @Override
    public Brand findById(Integer id) {
        return brandMapper.selectByPrimaryKey(id);
    }

    /**
     * 新增数据
     * @param brand
     * @return
     */
    @Override
    public Integer add(Brand brand) {
        return brandMapper.insertSelective(brand);
    }

    /**
     * 更新数据
     * @param brand
     * @return
     */
    @Override
    public Integer update(Brand brand) {
        return brandMapper.updateByPrimaryKeySelective(brand);
    }

    /**
     * 删除数据
     * @param id
     * @return
     */
    @Override
    public Integer dalete(Integer id) {
        return brandMapper.deleteByPrimaryKey(id);
    }

    /**
     * 列表条件查询
     * @param brand
     * @return
     */
    @Override
    public List<Brand> findList(Brand brand) {
        List<Brand> brandList = brandMapper.selectByExample(createExample(brand));
        return brandList;
    }

    /**
     * 列表分页查询
     * @param page
     * @param size
     */
    @Override
    public PageInfo<Brand> findPage(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        List<Brand> brandList = brandMapper.selectAll();
        return new PageInfo<>(brandList);
    }

    @Override
    public PageInfo<Brand> findPage(Brand brand, Integer page, Integer size) {
        PageHelper.startPage(page,size);
        List<Brand> brandList = brandMapper.selectByExample(createExample(brand));
        return new PageInfo<>(brandList);
    }

    /**
     * 根据商品分类id查询商品品牌列表
     * @param cid
     * @return
     */
    @Override
    public List<Brand> finByCid(Integer cid) {
        List<Brand> brandList= brandMapper.finByCid(cid);
        return brandList;
    }

}
